package controller;

import java.util.ArrayList;

import model.Customer;
import model.ItemContainer;
import model.Item;
import controller.OrderController;

public class StorageController {

	private static StorageController storageController = null;
	private static ItemContainer itemContainer;
	private static OrderController orderController;
	
	private StorageController(){
		itemContainer = ItemContainer.getInstance();
		orderController = OrderController.getInstance();	
	}
	
	public static StorageController getInstance(){
		if(storageController == null){
			storageController = new StorageController();
		}
		return storageController;
	}
	
	public boolean addItemToOrder(int itemId, int orderId){
		//orderController.addItemToOrder(itemContainer.findItem(String.valueOf(itemId)));
		int stock = getStock(String.valueOf(itemId));
		if(stock > 0) {
			stock -=1;
			setStock(String.valueOf(itemId),stock);
			orderController.addItemToOrder(itemId, orderId);
			return true;
		}
		else {
			return false;
		}
	}
	
	public int getId(String idName){
		return itemContainer.findItem(idName).getId();
	}
	
	public void setId(String idName, int newId){
		itemContainer.findItem(idName).setId(newId);
	}
	
	public String getName(String idName){
		return itemContainer.findItem(idName).getName();
	}
	
	public void setName(String idName, String name){
		itemContainer.findItem(idName).setName(name);
	}
	
	public String getDescription(String idName){
		return itemContainer.findItem(idName).getDescription();
	}
	
	public void setDescription(String idName, String description){
		itemContainer.findItem(idName).setDescription(description);
	}
	
	public int getStock(String idName){
		 return itemContainer.findItem(idName).getStock();
	}
	
	public void setStock(String idName, int stock){
		itemContainer.findItem(idName).setStock(stock);
	}
	
	public double getPrice(String idName){
		return itemContainer.findItem(idName).getPrice();
	}
	
	public void setPrice(String idName, double price){
		itemContainer.findItem(idName).setPrice(price);
	}
	
	public int getMinStock(String idName){
		return itemContainer.findItem(idName).getMinStock();
	}
	
	public void setMinStock(String idName, int minStock){
		itemContainer.findItem(idName).setMinStock(minStock);
	}
	
	//** Creates an Item object */
	public void createItem(int id, String name, String description, int stock, double price, int minStock){
		itemContainer.addItem(new Item(id, name, description, stock, price, minStock));
	}
	
	//** Updates an Item object */
	public void updateItem(String idName, int newId, String name, String description, int stock, double price, int minStock){
		Item item = itemContainer.findItem(idName);
		item.setId(newId);
		item.setName(name);
		item.setDescription(description);
		item.setPrice(price);
		item.setStock(stock);
		item.setMinStock(minStock);
	}
	
	//** Removes an Item object from itemContainer by id */
	public void removeItem(String idName){
		itemContainer.removeItem(itemContainer.findItem(idName));
	}
	
	public ArrayList<Object[]> findItems(String idName){
		ArrayList<Item> al = itemContainer.findItems(idName);
		ArrayList<Object[]>  alObject = new ArrayList<Object[]>();
		if(al.size() == 1){
			Item item = al.get(0);
			Object[] obj = { item.getId(), item.getName(), item.getDescription(), item.getPrice(), item.getStock(), item.getMinStock()};
			alObject.add(obj);
		} else if(al.size() > 1){
			for(Item item : al){
				Object[] obj = {item.getId(), item.getName(), item.getStock(), item.getPrice()};
				alObject.add(obj);
			}
		}
		return alObject;
	}
}